﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rattle
{
    public partial class Form1 : Form
    {
        //SoundPlayer player = new SoundPlayer();
        WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            int interval = (int) numericUpDown1.Value;
            timer1.Interval = (interval) * 1000;
            //player.SoundLocation = lblPath.Text;
            wmp.URL = lblPath.Text;

            Properties.Settings.Default.interval = interval;
            Properties.Settings.Default.path = lblPath.Text;
            Properties.Settings.Default.Save();
            timer1.Enabled = true;
            timer1_Tick(sender, e);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblPath.Text = Properties.Settings.Default.path;

            numericUpDown1.Value = Properties.Settings.Default.interval;
            timer1.Interval = Properties.Settings.Default.interval * 1000;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //string f = Path.Combine( Path.GetDirectoryName( Application.ExecutablePath ), "tada.wav" );

            //player.Play();
            wmp.URL = lblPath.Text;
            
        }

        private void lblPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            ofd.Filter = "wav files (*.wav)|*.wav|mp3 files (*.mp3)|*.mp3|All files (*.*)|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                lblPath.Text = ofd.FileName;
            }
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Visible = !this.Visible;
            if (this.WindowState != FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Normal;
            }

            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = MessageBox.Show(
                    "Really close app?", 
                    "Question", 
                    MessageBoxButtons.OKCancel, 
                    MessageBoxIcon.Question
                ) 
                != DialogResult.OK;
        }
    }
}
